﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace sivxbrowser
{   
    public partial class taskbarBrowser : Form
    {
        static int taskbarOffsetY = 32;
        static int taskbarOffsetX = 0;
        static int elapsedSecondsBeforeRefresh = 30;
        public taskbarBrowser()
        {
            InitializeComponent();
            SetDesktopLocation(Screen.PrimaryScreen.Bounds.Width - Width + taskbarOffsetX, Screen.PrimaryScreen.Bounds.Height - Height - taskbarOffsetY);
            //this.webBrowser1.DocumentText = "<meta name=\"sivxWindowWidth\" content=\"400\">" + "<meta name=\"sivxWindowHeight\" content=\"300\">";            
            Thread thread = new Thread(new ThreadStart(worker));
            thread.Start();
        }

        public void worker()
        {
            int elapsedSeconds = 0;
            while (true)
            {                
                try
                {
                    this.Invoke((MethodInvoker)delegate
                    {   
                        checkMetaValues();
                        if (elapsedSeconds > elapsedSecondsBeforeRefresh)
                        {
                            this.webBrowser1.Refresh();
                            elapsedSeconds = 0;
                        }
                    });
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
                elapsedSeconds += 2;
                System.Threading.Thread.Sleep(2000);
            }
        }

        public void checkMetaValues()
        {
            try
            {
                HtmlElementCollection metaCollection = this.webBrowser1.Document.GetElementsByTagName("meta");
                foreach (HtmlElement elem in metaCollection)
                {

                    switch (elem.GetAttribute("name"))
                    {
                        case "sivxWindowWidth":
                            Console.WriteLine("Width is: " + elem.GetAttribute("content"));
                            try
                            {
                                int width = Convert.ToInt32(elem.GetAttribute("content"));
                                ClientSize = new Size(width, Height);
                                SetDesktopLocation(Screen.PrimaryScreen.Bounds.Width - width + taskbarOffsetX, Screen.PrimaryScreen.Bounds.Height - Height - taskbarOffsetY);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e);
                            }
                            break;
                        case "sivxWindowHeight":
                            Console.WriteLine("Height is: " + elem.GetAttribute("content"));
                            try
                            {
                                int height = Convert.ToInt32(elem.GetAttribute("content"));
                                ClientSize = new Size(Width, height);
                                SetDesktopLocation(Screen.PrimaryScreen.Bounds.Width - Width + taskbarOffsetX, Screen.PrimaryScreen.Bounds.Height - height - taskbarOffsetY);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine(e);
                            }
                            break;
                    }
                }
                //Console.WriteLine(this.webBrowser1.DocumentText);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {   
            //Console.WriteLine(e.Button.ToString() + " was pressed on the icon");
            //this.webBrowser1.DocumentText = "Hi";
            //Console.WriteLine("." + this.webBrowser1.DocumentText + ".");
            Activate();
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            Activate();
            if (Visible) Visible = false;
            else Visible = true;
        }
    }
}
